package com.example.hotel_lxy.ui.dialog;

import android.app.Dialog;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.viewpager.widget.PagerAdapter;
import androidx.viewpager.widget.ViewPager;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.target.SimpleTarget;
import com.bumptech.glide.request.transition.Transition;
import com.example.hotel_lxy.R;
import com.example.hotel_lxy.view.TouchImageView;

import java.util.ArrayList;
import java.util.List;

import uk.co.senab.photoview.PhotoView;
import uk.co.senab.photoview.PhotoViewAttacher;


/**
* 图片查看 （多图）
* create by Sincerly on 9999/9/9 0009
**/
public class ImageViewpagerDialog extends Dialog {
    private Context mContext;
    private OnDialogClickListener listener;
    private static List<String> imgs;
    private static  int num;

    List<String> img=new ArrayList<>();
    private List<PhotoView> mImageList = new ArrayList<PhotoView>();
    public ImageViewpagerDialog(@NonNull Context context, int themeResId) {
        super(context, themeResId);
        this.mContext = context;
//        init();
    }

    private View init() {
        View view = View.inflate(mContext, R.layout.dialog_view_img, null);
        ViewPager viewPager=view.findViewById(R.id.viewPager);
        TextView text_title=view.findViewById(R.id.text_title);
        PhotoView imageView;
        for (int i = 0; i <imgs.size() ; i++) {
            if(!imgs.get(i).equals("")){
                img.add(imgs.get(i));
            }
        }
        for (int i = 0; i <img.size() ; i++) {
            imageView = new PhotoView(mContext);
            //添加到数组
            mImageList.add(imageView);
        }

        DemoPagerAdapter  mAdapter = new DemoPagerAdapter();
        viewPager.setAdapter(mAdapter);
        viewPager.setCurrentItem(num);
        text_title.setText((num+1)+"/"+img.size());
        viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                text_title.setText((position+1)+"/"+img.size());
            }

            @Override
            public void onPageSelected(int position) {

            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
//        view.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                dismiss();
//            }
//        });
        return view;
    }




    public OnDialogClickListener getListener() {
        return listener;
    }

    public void setListener(OnDialogClickListener listener) {
        this.listener = listener;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setCanceledOnTouchOutside(true);
        setContentView(init());
    }

    public void showDialog() {
        if (!isShowing()) {
            show();
            WindowManager.LayoutParams lp = getWindow().getAttributes();
//            lp.height = DisplayUtils.getDisplayWidth(mContext) * 5 / 5;
//            lp.width = DisplayUtils.getDisplayWidth(mContext) * 10 / 10;
            lp.height = WindowManager.LayoutParams.MATCH_PARENT;
            lp.width = WindowManager.LayoutParams.MATCH_PARENT;
            getWindow().setAttributes(lp);
            getWindow().setGravity(Gravity.BOTTOM);
        }
    }

    public static ImageViewpagerDialog show(Context context, OnDialogClickListener listener, List<String> img,int nums) {
        ImageViewpagerDialog dialog = new ImageViewpagerDialog(context, R.style.BottomDialogStyle);
        dialog.setListener(listener);
        imgs=img;
        num=nums;
        dialog.showDialog();
        return dialog;
    }

    public interface OnDialogClickListener {
        void sure();
    }
    public class DemoPagerAdapter extends PagerAdapter {

        @Override
        public int getCount() {
            return mImageList.size();
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            //获取图片view
            PhotoView imageView = mImageList.get(position);
//            imageView.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
            Glide.with(mContext).load(img.get(position)).into(imageView);
            //设置到容器,也就是ViewPager
            container.addView(imageView);
            imageView.setOnPhotoTapListener(new PhotoViewAttacher.OnPhotoTapListener() {
                @Override
                public void onPhotoTap(View view, float x, float y) {
                    dismiss();
                }

                @Override
                public void onOutsidePhotoTap() {

                }
            });
            //返回控件
            return imageView;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView( mImageList.get(position));
        }
    }

}